import { _decorator, Color, Component, EventTouch, Input, Node, Sprite, SpriteAtlas, SpriteFrame, Tween, tween, UITransform, v2, v3, Vec2, Vec3 } from 'cc'
import { ConfigWeapItemData } from '../ConfigShapItemData'
import { GameTools } from '../../../Utils/GameTools'
import { weapMgr } from '../Mgr/WeapManager'
import EventManager from '../../../Runtime/EventManager'
import { GameEvent } from '../../../Runtime/GameEvent'
import { GridMgr } from '../Mgr/GrildManager'

const { ccclass } = _decorator

@ccclass('WeapItem')
export class WeapItem extends Component {

  weapMapOnGrid: Vec2[] = []

  /**是否被选择 */
  _isTouch: boolean = false

  /**放置格子的下表数组 */
  _indexOnGrid: number[] = []


  public m_weapItemData: ConfigWeapItemData = null

  public point: Node = null;
  public icon: Node = null;
  // 设置_setPosIndex的值
  set indexOnGrid(value) {
    this._indexOnGrid = value
  }
  get indexOnGrid() {
    return this._indexOnGrid
  }

  protected onEnable(): void {
    this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
    this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
    this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);

    EventManager.Instance.on(GameEvent.WEAPON_SHAKE, this.onSetIconShake, this)
    EventManager.Instance.on(GameEvent.WEAP_STOP_ANi, this.onShakestop, this)
    EventManager.Instance.on(GameEvent.WEAPON_UPGRADE, this.weapUpgrade, this)

  }
  protected onDisable(): void {
    this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
    this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
    this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);

    EventManager.Instance.off(GameEvent.WEAPON_SHAKE, this.onSetIconShake);
    EventManager.Instance.off(GameEvent.WEAP_STOP_ANi, this.onShakestop);
    EventManager.Instance.off(GameEvent.WEAPON_UPGRADE, this.weapUpgrade);
  }
  createWeapItem(ItemData: ConfigWeapItemData) {
    this.point = this.node.getChildByName('point');
    this.icon = this.node.getChildByName('icon');

    this.m_weapItemData = ItemData

    const sprite = this.icon.addComponent(Sprite)
    let spriteframe = this.m_weapItemData.icon
    sprite.spriteFrame = weapMgr.atals.getSpriteFrame(spriteframe)
    //sizeMap以*号分隔
    const sizeMapStr = this.m_weapItemData.sizeMap
    const arr = sizeMapStr.split(';')
    for (let i = 0; i < arr.length; i++) {
      this.weapMapOnGrid[i] = GridMgr.getPosBySizeMap(arr[i]);
    }
    /**生成唯一的key */
    const weapKey = GameTools.GenNonDuplicateID(10);
    this.m_weapItemData.weapKey = weapKey;

    //设置旋转角度
    this.node.eulerAngles = new Vec3(0, 0, GameTools.RandomByRange(0, 85))
    this.icon.setScale(new Vec3(this.m_weapItemData.nodeScale, this.m_weapItemData.nodeScale, 1))


    this.initWeaponSize()
  }
  /* 初始武器格子尺寸 */
  initWeaponSize() {
    let typeArr = this.m_weapItemData.col_row.split('_');
    let rowLen = Number(typeArr[1]);
    let colLen = Number(typeArr[0]);
    let size = GridMgr.getGridSizeByRowCol(rowLen, colLen);
    this.node.getComponent(UITransform).setContentSize(size);
    this.point.setPosition(-size.width / 2, -size.height / 2);
  }
  setWeapParent(parent: Node) {
    this.node.parent = parent
  }
  getWeapGridMap() {
    return this.weapMapOnGrid
  }

  private onTouchStart(event: EventTouch) {
    this.node.eulerAngles = new Vec3(0, 0, 0)
    const target = event.target as Node
    if (target) {
      this._isTouch = true;
      EventManager.Instance.emit(GameEvent.WEAPON_CLICK, this.node)
    }
  }

  private onTouchMove(event: EventTouch) {
    if (this._isTouch) {
      EventManager.Instance.emit(GameEvent.ADD_WEAP_MOVE)
      let wpos = event.getUILocation();
      let pos = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(wpos.x, wpos.y, 0));
      this.node.setPosition(pos.x, pos.y);

      if (this.m_weapItemData.type == 0) {
        return
      }

      EventManager.Instance.emit(GameEvent.PLACE_WEAP_MOVING)
    }
  }

  private onTouchEnd(event: EventTouch) {
    if (this._isTouch) {
      EventManager.Instance.emit(GameEvent.PLACE_WEAP_END)
      this._isTouch = null;
    }
  }
  _isShake: boolean = false;
  onSetIconShake(weapKey: string) {
    if (this.m_weapItemData.weapKey == weapKey && !this._isTouch) {
      //抖动
      if (!this._isShake) {
        this._isShake = true;
        let posX = 5;
        let time = 0.1;
        this.icon.setPosition(0, 0, 0);
        tween(this.icon).by(time, { position: v3(-posX, 0, 0) }).by(time, { position: v3(posX, 0, 0) }).by(time, { position: v3(posX, 0, 0) }).by(time, { position: v3(-posX, 0, 0) })
          .union().repeatForever().start();
      }
    }
  }
  weapUpgrade() {
    //TODO
  }
  onShakestop() {
    if (this._isShake) {
      Tween.stopAllByTarget(this.icon);
      this._isShake = false;
    }
  }
  setDeafult() {
    this.icon.getComponent(Sprite).color = new Color().fromHEX('FFFFFF');
  }

  setGreen() {
    this.icon.getComponent(Sprite).color = new Color().fromHEX('7ed263');
  }

  setRed() {
    this.icon.getComponent(Sprite).color = new Color().fromHEX('dd6662');
  }

  setYellow() {
    this.icon.getComponent(Sprite).color = new Color().fromHEX('dbc464');
  }

  update(deltaTime: number) { }
}
